package com.personalblog.service.serviceImpl;

import com.personalblog.mapper.UserMapper;
import com.personalblog.po.User;
import com.personalblog.service.UserService;
import com.personalblog.utils.MD5Utils;
import com.personalblog.vo.UserPermissionVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/2/22 - 20:50
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryAll() {
        List<User> userList = new ArrayList<>();
        try {
            userList = userMapper.queryAll();
            logger.info("查询成功！");
            if (userList == null){
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public User findUserByUsernameAndPassword(String username,String password) {
        User user = new User();
        try {
            user = userMapper.findUserByUsernameAndPassword(username, MD5Utils.code(password));
            logger.info("根据用户名和密码查询成功！");
            if (user == null){
                logger.info("根据用户名和密码查询为空值！");
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User findUserByUserName(String userName) {
        User user = new User();
        try {
            user = userMapper.findUserByUserName(userName);
            logger.info("根据用户名查询成功！");
            if (user == null){
                logger.info("根据用户名查询为空值！");
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<UserPermissionVo> queryUserPermission(String userName) {
        User user = userMapper.findUserByUserName(userName);
        logger.info("根据用户名查询成功！");
        if (user == null){
            logger.info("根据用户名查询为空值！");
            return null;
        }
        List<UserPermissionVo> list = userMapper.queryUserPermission(user.getUserId());
        return list;
    }


}
